﻿#include "hpdf.h"
#include <iostream>
#include <string>
#include <fstream>
#include <stdexcept>
#include <sstream>
#include <vector>
#include <iomanip>

#define TEST

std::string convertToUTF8(int unicode) {
    std::string result;

    if (unicode <= 0x7F) {
        result += static_cast<char>(unicode); // 1-byte UTF-8
    }
    else if (unicode <= 0x7FF) {
        result += static_cast<char>(0xC0 | ((unicode >> 6) & 0x1F)); // 1st byte
        result += static_cast<char>(0x80 | (unicode & 0x3F)); // 2nd byte
    }
    else if (unicode <= 0xFFFF) {
        result += static_cast<char>(0xE0 | ((unicode >> 12) & 0x0F)); // 1st byte
        result += static_cast<char>(0x80 | ((unicode >> 6) & 0x3F)); // 2nd byte
        result += static_cast<char>(0x80 | (unicode & 0x3F)); // 3rd byte
    }
    else if (unicode <= 0x10FFFF) {
        result += static_cast<char>(0xF0 | ((unicode >> 18) & 0x07)); // 1st byte
        result += static_cast<char>(0x80 | ((unicode >> 12) & 0x3F)); // 2nd byte
        result += static_cast<char>(0x80 | ((unicode >> 6) & 0x3F)); // 3rd byte
        result += static_cast<char>(0x80 | (unicode & 0x3F)); // 4th byte
    }
    else {
        // Invalid unicode value
        return ""; // or handle the error as needed
    }

    return result;
}

int main() {
    std::ifstream infile("text3.txt");
    if (!infile.is_open()) {
        std::cerr << "Error: cannot open text.txt" << std::endl;
        return 1;
    }

    std::vector<uint32_t> unicode_values;
    std::string line;

    while (std::getline(infile, line)) {
        // 去掉行尾的换行符和空白符
        line.erase(line.find_last_not_of(" \n\r\t") + 1);

        // 将十六进制字符串转换成 uint32_t
        uint32_t value;
        std::stringstream ss;
        ss << std::hex << line;
        ss >> value;

        unicode_values.push_back(value);
    }

    infile.close();


    const int line_count_per_page = 20;
    const int char_count_per_line = 15;
    const int char_count_per_page = 20 * 15;
    int line_count = 0;
    int char_count = 0;

    // 创建 PDF 文档
    HPDF_Doc pdf = HPDF_New(NULL, NULL);
    if (!pdf) {
        std::cerr << "Error: cannot create PDF object" << std::endl;
        return 1;
    }

    // 加载字体
    HPDF_UseUTFEncodings(pdf);
    HPDF_SetCurrentEncoder(pdf, "UTF-8");
    HPDF_Font font = HPDF_GetFont(pdf, HPDF_LoadTTFontFromFile(pdf, "simsun.ttf", HPDF_TRUE), "UTF-8");

    if (!font) {
        std::cerr << "Error: cannot load font" << std::endl;
        HPDF_Free(pdf);
        return 1;
    }

    // 创建画布
    HPDF_Page page = nullptr;
    float x, y;


    for (auto elem : unicode_values) {
        if (char_count % char_count_per_page == 0) {
			page = HPDF_AddPage(pdf);
			HPDF_Page_SetFontAndSize(page, font, 12);
			HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
			x = 100;
			y = 800;
        }
        if (char_count % char_count_per_line == 0) {
            y -= 30;
            x = 100;
            line_count++;
        }
 
        std::string utf8String = convertToUTF8(elem); // 转换为 UTF-8 字符串

        // 显示字符
        HPDF_Page_BeginText(page);
        HPDF_Page_MoveTextPos(page, x, y);
        HPDF_Page_ShowText(page, utf8String.c_str()); // 使用 UTF-8 字符串
        HPDF_Page_EndText(page);
        char_count++;
        x += 20;

    }

    // 保存 PDF 文件
    HPDF_SaveToFile(pdf, "output.pdf");
    
    // 清理资源
    HPDF_Free(pdf);

    std::cout << "PDF 文件已生成。" << std::endl;
    return 0;
}